// 封装分类数据业务相关代码
import {getCategoryAPI} from '@/apis/category'
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import { onBeforeRouteUpdate } from 'vue-router'
export function useCategory(){
    const categoryData=ref([])
    const route=useRoute()
    const getCategory=async(id=route.params.id)=>{
        const res=await getCategoryAPI(id)
        categoryData.value=res.result
    }
    onMounted(()=>getCategory())

    //目标：路由参数变化时，将分类接口数据重新发送
    onBeforeRouteUpdate((to)=>{
    // console.log('路有变化了。。')
    getCategory(to.params.id)
    })

    return {
        categoryData
    }
}